package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Servicio.MantProve_Service;
import bean.Proveedor;

@SuppressWarnings("serial")
public class MantenProve_Servlet extends HttpServlet {

	 private MantProve_Service servicio = new MantProve_Service(); 
	 
	 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 String operacion = request.getParameter("operacionMantProv");

		if(operacion.equals("Agregar")){
				this.registrar(request,response);
		}
		else if(operacion.equals("Actualizar")){
				this.actualizar(request,response);
		}
		else if(operacion.equals("desactivar")){
				this.desactivar(request,response);
		}
		else if(operacion.equals("Buscar")){
			this.Buscar(request,response);
		}
		 

	}
	
	private void registrar(HttpServletRequest request,HttpServletResponse response)throws ServletException {

		try {
			String  RUC = request.getParameter("txtRUC");
			String  RazSoc = request.getParameter("txtRazSoc");
			String  Direccion = request.getParameter("txtDirec");
			String  NomCont = request.getParameter("txtNomCont");
			String  Telefono =request.getParameter("txtTelef");
			String  Correo = request.getParameter("txtCorreo");
			String  Cargo = request.getParameter("txtCargo");
			String  Estado = request.getParameter("txtEstado");		
			
			Proveedor obj = new Proveedor(RUC,RazSoc,Direccion,NomCont,Telefono, Correo,Cargo,Estado);
			servicio.RegistrarProveedor(obj);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	private void actualizar(HttpServletRequest request,HttpServletResponse response)throws ServletException {

		try {
			String  RUC = request.getParameter("txtRUC");
			String  RazSoc = request.getParameter("txtRazSoc");
			String  Direccion = request.getParameter("txtDirec");
			String  NomCont = request.getParameter("txtNomCont");
			String  Telefono =request.getParameter("txtTelef");
			String  Correo = request.getParameter("txtCorreo");
			String  Cargo = request.getParameter("txtCargo");
			String  Estado = request.getParameter("txtEstado");		
			
			Proveedor obj = new Proveedor(RUC,RazSoc,Direccion,NomCont,Telefono, Correo,Cargo,Estado);
			servicio.ActualizarProveedor(obj);
			confirmar(request,response);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	private void desactivar(HttpServletRequest request,HttpServletResponse response)throws ServletException {

		try {
					
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	private void Buscar(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		
		Proveedor objPro = new Proveedor(null, request.getParameter("txtRazonSoc"), null, null, null, null, null, null);

		try{
			List<Proveedor> lista = servicio.ListarProveedores(objPro);
			request.setAttribute("listadito", lista);
			request.getRequestDispatcher("/frm_MantenProveedor.jsp").forward(request, response);

		}catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	
	
	private void confirmar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out= response.getWriter();
    	out.println("<html>");
		out.println("<head>");
		out.println("<title>prueba de verificacion</title>");
		out.println("<head>");
		out.println("<body>");
		out.println("<H2> Entro a Eliminar</H2>");
		out.println("</body>");
		out.println("</html>");
	}
	
	
}
























































